import {
  mapState,
  mapMutations,
  mapActions
} from "vuex";



import {
  getMessageList
} from "@/assets/api/userCenterApi";

import {
  getLawWindow
} from "@/assets/api/indexApi";

import {
  goTopSmooth,
  IEVersion,
} from "@/assets/utils/utils.js";


export default {
  computed: {
    ...mapState("common", {
      noViewShow: (state) => state.noViewShow,
      userInfo: (state) => state.userInfo,
      zim: (state) => state.zim,
      goeasyObj: (state) => state.goeasyObj,
    }),
  },
  data() {
    return {
      showButton: false,
      // lawWindowObj: null
    };
  },
  mounted() {

    // 检测是否IE
    // if (!IEVersion()) {
    //   this.isIE = true;
    // }

    // this.checkHref();

    // 获取公告活动未读数
    this.getMessageList()

    // 获取首页法规库推送弹窗
    this.getLawWindow()

    this.$nextTick(() => {
      window.addEventListener("scroll", this.checkScroll);
    });

  },
  beforeDestroy() {
    this.beforeunloadHandler();
    window.removeEventListener("scroll", this.checkScroll);
  },
  methods: {
    ...mapMutations("common", [
      "set_messageCount2",
      "noLookShool",
    ]),

    async getLawWindow() {
      const res = await getLawWindow({})
      if (res.code == 1) {
        let lawWindowObj = res.data
        let id = localStorage.getItem("libraryPopupId");

        if (id == res.data.go_id) {

        } else {
          this.$alert(`标题：${lawWindowObj.name}`, `${lawWindowObj.type}更新啦！`, {
            dangerouslyUseHTMLString: true,
            confirmButtonText: '查看详情'
          }).then(() => {
            let url = ''
            if (lawWindowObj.type === '会计法规库') {
              url = `/kjfgk/${lawWindowObj.go_id}.html`
            } else if (lawWindowObj.type === '税收法规库') {
              url = `/ssfgk/${lawWindowObj.go_id}.html`
            } else if (lawWindowObj.type === '优惠政策') {
              url = `/ssfgkyh/${lawWindowObj.go_id}.html`
            }
            if (url)
              window.open(url, '_blank')

          });
          localStorage.setItem("libraryPopupId", res.data.go_id);
        }
      }
    },


    goTopSmooth() {
      goTopSmooth()
    },

    // 判断页面高度变化值
    checkScroll() {
      const scrollHeight = window.scrollY;
      if (scrollHeight > 520) {
        this.showButton = true;
      } else {
        this.showButton = false;
      }
    },

    checkHref() {
      let host = window.location.host;

      let protocol = window.location.protocol;

      var url = document.location.toString();
      var arrUrl = url.split("//");

      var start = arrUrl[1].indexOf("/");
      var relUrl = arrUrl[1].substring(start); //stop省略，截取从start开始到结尾的所有字符
      if (protocol == "http:" && host == "www.xzcs2022.com") {
        window.location.replace("https://www.xzcs2022.com" + relUrl);
      }
      if (
        host == "www.xzcs2022.com" ||
        host == "dev-pc.xiaozhucaishui.com" ||
        host == "127.0.0.1:5173" ||
        host == "124.221.189.244" ||
        host == "123.56.89.141" ||
        host == "dev-pc.xzcs2022.com" ||
        host == "localhost:3000" ||
        host == "82.156.202.241" ||
        host == "test-pc.xiaozhucaishui.com"
      ) {} else {
        window.location.replace("https://www.xzcs2022.com" + relUrl);
      }
    },

    // 关闭页面操作
    beforeunloadHandler(e) {
      if (this.goeasyObj && this.userInfo)
        this.goeasyObj.pubsub.unsubscribe({
          channel: this.userInfo.id,
          onSuccess: function () {},
          onFailed: function (error) {},
        });
      if (this.$route.path == '/service/servicechat') {
        localStorage.removeItem("zimStatus")
      }
    },

    // 获取公告 活动未读
    async getMessageList() {
      if (!localStorage.getItem("token")) return;
      let xx_unread = localStorage.getItem("xx_unread");
      let hd_unread = localStorage.getItem("hd_unread");


      if (!xx_unread || !hd_unread) {
        localStorage.setItem("xx_unread", 0);
        localStorage.setItem("hd_unread", 0);
        return;
      }

      const res1 = await getMessageList({
        type: 1,
      });
      const res2 = await getMessageList({
        type: 2,
      });
      if (res1.code == 1 && res2.code == 1) {
        let resLength1 = res1.data.length - parseInt(xx_unread);
        let resLength2 = res2.data.length - parseInt(hd_unread);
        if (resLength1 > 0) {
          localStorage.setItem("xx_unread", res1.data.length);
          this.set_messageCount2({
            xx_unread: resLength1
          });
        }
        if (resLength2 > 0) {
          localStorage.setItem("hd_unread", res2.data.length);
          this.set_messageCount2({
            hd_unread: resLength2
          });
        }
      }
    },



  }
};
